package de.baumann.browser.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import de.baumann.browser.R
import de.baumann.browser.db.ETHWallet
import de.baumann.browser.utils.web3j.BalanceTask

class EthSelectAdapter(var context: Context, var wallets: List<ETHWallet>) : BaseAdapter() {
    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
        val view = LayoutInflater.from(context).inflate(R.layout.item_sp_select, p2, false)
        if (view != null) {
            val tvAddress: TextView = view.findViewById(R.id.tvOdinAddress)
            val tvBalance: TextView = view.findViewById(R.id.tvOdinBalance)
            var address = wallets[p0].address
            if (address.startsWith("0x")) {
                address = "od${address.substring(1)}"
            }
            tvAddress.text = address
            BalanceTask(tvBalance).execute(wallets[p0].address)
        }
        return view
    }

    override fun getItem(p0: Int): Any {
        return wallets[p0]
    }

    override fun getItemId(p0: Int): Long {
        return p0.toLong()
    }

    override fun getCount(): Int {
        return wallets.size
    }

}